package com.mpgame.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DataSourceConfig {
    // Verify数据源
    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.verify")
    public DataSource verifyDataSource() {
        return DataSourceBuilder.create().build();
    }

    // Game数据源
    @Bean
    @ConfigurationProperties("spring.datasource.game")
    public DataSource gameDataSource() {
        return DataSourceBuilder.create().build();
    }

    // Verify数据库的SqlSessionFactory
    @Bean
    @Primary
    public SqlSessionFactory verifySqlSessionFactory(@Qualifier("verifyDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.mpgame.database.mysql.model");

        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        factoryBean.setConfiguration(configuration);

        return factoryBean.getObject();
    }

    // Game数据库的SqlSessionFactory
    @Bean
    public SqlSessionFactory gameSqlSessionFactory(@Qualifier("gameDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage("com.mpgame.database.mysql.model");

        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        factoryBean.setConfiguration(configuration);

        return factoryBean.getObject();
    }

    // Verify数据库事务管理器
    @Bean
    @Primary
    public PlatformTransactionManager verifyTransactionManager(@Qualifier("verifyDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    // Game数据库事务管理器
    @Bean
    public PlatformTransactionManager gameTransactionManager(@Qualifier("gameDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
